/**   
* @Package com.wlyy.bcwlw.sys.area.web 
*/
package com.wlyy.bcwlw.sys.area.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wlyy.bcwlw.base.BaseController;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.sys.area.dao.AreaCityDAO;
import com.wlyy.bcwlw.sys.area.dao.AreaDAO;
import com.wlyy.bcwlw.sys.area.dao.AreaDistrictDAO;
import com.wlyy.bcwlw.sys.area.dao.AreaProvinceDAO;
import com.wlyy.bcwlw.sys.area.entity.AreaDTO;
import com.wlyy.bcwlw.sys.area.service.AreaService;
import com.wlyy.bcwlw.sys.user.dao.UserDAO;
import com.wlyy.bcwlw.sys.user.entity.UserDTO;

/**
 * @ClassName. SysAreaController
 * @Description. TODO
 * @author zhangyang
 * @date 2016年1月18日 上午10:48:06
 * @version V1.0
 */
@RemoteProxy
@Controller
@RequestMapping("/sys/area")
public class SysAreaController extends BaseController{

	@Autowired
	private AreaService areaService;
	
	@Autowired
	private AreaDAO areaDAO;
	
	@Autowired
	private AreaProvinceDAO areaProvinceDAO;
	@Autowired
	private AreaCityDAO areaCityDAO;
	@Autowired
	private AreaDistrictDAO areaDistrictDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	
	@RequestMapping("/index")
	public String areaView(HttpServletRequest request, HttpServletResponse response){
		set(request,"areaProvinceList",areaProvinceDAO.queryListByBatchCriteria(null, " province_id ", null, null));
		set(request,"areaCityList",areaCityDAO.queryListByBatchCriteria(null, " city_id ", null, null));
		set(request,"areaDistrictList",areaDistrictDAO.queryListByBatchCriteria(null, " district_id ", null, null));
		return "sys/area/area-index";
	}
	
	@RequestMapping("/area-radio-tree")
	public String areaRadioTree(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String areaId = get(request,"areaId");
		set(request,"areaId",areaId);
		return "common/area/area-radio-tree";
	}
	

	@RequestMapping("/user_list")
	public String userListByAreaId(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int curPage = getPageNum(request);
		int limit = getInternal(request);
		
		String areaId = get(request,"areaId");
		if(StringUtils.isEmpty(areaId)){
			return null;
		}
		
		
		List<UserDTO> userList = userDAO.queryUserInfo(areaId);
		
		int count = userList.size();
		
		processJsonData4Grid(request, response, limit, curPage, count, userList);
		
		return null;
	}
	
	@RequestMapping("/user_noarea_list")
	public String userNoAreaList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int curPage = getPageNum(request);
		int limit = getInternal(request);
		
		List<UserDTO> userList = userDAO.queryUserWithNoArea();
		
		int count = userList.size();
		
		processJsonData4Grid(request, response, limit, curPage, count, userList);
		
		return null;
	}
	
	@RequestMapping("/adduser")
	public String userAddOpen(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String areaId = get(request,"areaId");
		set(request,"areaId",areaId);
		return "sys/area/area-add-user";
	}
	
	public AreaDTO getAreaById(String areaId){
		return areaService.getAreaById(areaId);
	}
	
	public TreeNode getAreaTreeById(String id){
		return areaService.getAreaTreeById(id);
	}
	
	public String removeAreaById(String id){
		return areaService.removeAreaById(id);
	}
	
	public AreaDTO saveArea(AreaDTO areaDTO){
		return areaService.saveArea(areaDTO);
	}
	
	
	/**
	 * 
	* @Title. addUserIntoAreaByUserId
	* @Description. 添加地区人员
	* @param areaId
	* @param userId
	* @return boolean
	* @exception.
	 */
	public boolean addUserIntoAreaByUserId(String areaId,String userId){
		UserDTO user = userDAO.findById(userId);
		if(user == null){
			return false;
		}
		user.setUserAreaId(areaId);
		userDAO.update(user);
		return true;
	}
	
	/**
	 * 
	* @Title. removeUserAreaInfoByUserArray
	* @Description. 删除地区人员
	* @param userIds
	* @return boolean
	* @exception.
	 */
	public boolean removeUserAreaInfoByUserArray(String[] userIds){
		if(userIds == null || userIds.length == 0){
			return false;
		}
		userDAO.setUserAreaIsNull(userIds);
		return true;
	}
	
	
}
